//搜索栏组件
<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col class="left" :span="3">
        <img
          src="//s0.meituan.net/bs/fe-web-meituan/10afbf1/img/logo.png"
          alt="美团"
        />
      </el-col>
      <el-col class="center" :span="15">
        <!-- 搜索框区域 -->
        <div class="wrapper">
          <!-- @focus 获得焦点触发事件 -->
          <!-- @blur 失去焦点触发事件 -->
          <!-- @input 监听输入事件-->
          <el-input
            v-model="search"
            placeholder="搜索商家或者地点"
            @focus="handleFocus"
            @blur="handleBlur"
            @input="handleInput"
          ></el-input>
          <button class="el-button el-button--primary">
            <i class="el-icon-search"></i>
          </button>
          <!-- 热门搜索列表 -->
          <dl class="hot-search" v-if="isHotSearch">
            <dt>热门搜索</dt>
            <dd v-for="(item, index) in hotSearch" :key="index">{{ item }}</dd>
          </dl>
          <!-- 搜索结果提示列表 -->
          <dl class="search-list" v-if="isSearchList">
            <dd v-for="(item, index) in searchList" :key="index">{{ item }}</dd>
          </dl>
        </div>
        <p class="suggest">
          <a href="#">故宫博物院</a>
          <a href="#">故宫博物院</a>
          <a href="#">故宫博物院</a>
          <a href="#">故宫博物院</a>
          <a href="#">故宫博物院</a>
        </p>
        <ul class="nav">
          <li>
            <nuxt-link to="/" class="takeout">美团外卖</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="movie">猫眼电影</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="hotel">美团酒店</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="apartment">民宿/公寓</nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="business">商家入驻</nuxt-link>
          </li>
        </ul>
      </el-col>
      <el-col class="right" :span="6">
        <ul class="security">
          <li><i class="refund">随时退</i></li>
          <li><i class="free">不满意免单</i></li>
          <li><i class="overdue">过期退</i></li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from "lodash";

export default {
  data() {
    return {
      //记录输入框里内容
      search: "",
      // 是否聚焦
      isFocus: false,
      hotSearch: ["欢乐谷", "欢乐谷", "欢乐谷"],
      searchList: ["颐和园", "颐和园", "颐和园"],
    };
  },
  computed: {
    isHotSearch() {
      // 聚焦但没有输入内容时返回true
      return this.isFocus && !this.search;
    },
    isSearchList() {
      // 聚焦但有输入内容时返回true
      return this.isFocus && this.search;
    },
  },
  methods: {
    handleFocus() {
      this.isFocus = true;
    },
    handleBlur() {
      let _self = this;
      setTimeout(() => {
        _self.isFocus = false;
      }, 200);
    },
    //延迟发送请求
    handleInput: _.debounce(async function () {
      let city = this.$store.state.geo.position.city.replace("市", "");
      // this.searchList = [];

      if (this.search.length) {
        //请求数据
        //这里后端API挂了，无法请求数据
        // let {
        //   status,
        //   data: { top },
        // } = await this.$axios.get("/search/top", {
        //   params: {
        //     input: this.search,
        //     city: city,
        //   },
        // });
        // this.searchList = top.slice(0, 10);
      }
    }, 300),
  },
};
</script>